const qs = require("../dist/qs");

const BaseUrl = 'http://123.57.224.37:8888' // 地址

// const BaseUrl = 'http://10.8.20.238:8888'
// const BaseUrl = 'https://www.bodou.top' // 地址


function createRequest(url, method, params, qstate, contentType, state) {
	let dataVal = ''
	if (qstate) {
		dataVal = qs.stringify(params)
	} else {
		dataVal = params
	}
	let promise = new Promise((resolve, reject) => {
		uni.request({
			url: BaseUrl + url,
			data: dataVal || {},
			method: method,
			header: {
				'content-type': contentType ? contentType : 'application/json', // 默认值
				'x-farm-token': uni.getStorageSync('token')
			},
			success: (res) => {
				console.log('res11111', res)
				if (res.data.code == 50000) {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
					return
				}
				if (res.data.code == 2401) {
					uni.removeStorageSync('token');
					uni.showModal({
						title: "提示",
						content: "此操作需要登录",
						confirmText: "确定",
						cancelText: "取消",
						confirmColor: "#000",
						success: function (res) {
							if (res.confirm) {
								setTimeout(() => {
									uni.navigateTo({
										url: "/pages/login/login",
									})
								}, 500)
							} else if (res.cancel) {
								console.log('用户点击取消')
							}
						},
					})
				} else {
					resolve(res.data)
				}

			},
			fail: (fail) => {
				console.log('fail', fail)
				return reject(fail);
			}


		})
	})
	return promise;
}
const request = {
	get(url, params, state, contentType) {
		return createRequest(url, 'get', params, state, contentType)
	},
	post(url, params, state, contentType) {
		return createRequest(url, 'post', params, state, contentType)
	}
}
export default request
export {
	BaseUrl
}
